home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / COMM / INTERNET / TELNET / RLOGIND / !RloginD / telnet / term_c < prev    next >
Text File  |  1993-09-02  |  6KB  |  201 lines

  1. /* > term.c
  2.  * (C) 1993 Andrew Brooks
  3.  * arb@comp.lancs.ac.uk
  4.  * 1.00 Fri Feb  5 11:51:16 GMT 1993 - First incarnation
  5.  * 1.10 Wed May 26 12:10:10 BST 1993 - Term_Blocking added, fcntl altered
  6.  * 1.11 Wed May 26 12:24:37 BST 1993 - Term_NoCtrlTerm now uses setsid
  7.  * 1.12 Wed May 26 21:31:14 BST 1993 - Term_Ready added
  8.  * 1.13 Thu Jun  3 22:04:52 BST 1993 - Term_Spool added
  9.  * 1.20 Thu Sep  2 11:46:59 BST 1993 - Special version for rxtelnet
  10.  */
  11.  
  12. static char SCCSid[] = "@(#)term.c        1.20 (C) 1993 arb Terminal library";
  13.  
  14. /*
  15.  * System-independent terminal handling library.
  16.  * When compiling this library the appropriate flags must be set:
  17.  * TERMIO
  18.  *   This is typically on System III and System V (and SunOS)
  19.  *   Look for a termio.h header file and termio manual page
  20.  *   #define TERMIO
  21.  * SGTTY
  22.  *   This is typically on Version 7 systems
  23.  *   Look for a sgtty.h header file
  24.  *   #define SGTTY
  25.  * ITTY
  26.  *   This is typically on BSD systems
  27.  *   Look for a ioctl.h header file and a tty(4) manual page
  28.  *   #define ITTY
  29.  *   struct tchars is also used to contain special characters.
  30.  *   ioctls TIOCLBI[SC] also used for more flags.
  31.  *   struct ltchars also used with *more* flags!  What a mess.
  32.  * If more than one of the above applies (eg.SunOS) then use TERMIO
  33.  * If none of the above apply then you are stuck.
  34.  */
  35.  
  36. /*
  37.  * All of these functions take a file descriptor as the first parameter.
  38.  * This is an integer identifying the stream to be operated on.  If using
  39.  * stdio then the FILE * should be converted to a fd using .
  40.  */
  41.  
  42. #include "term.h"
  43.  
  44. /* Simple error check */
  45. #ifndef TERMIO
  46. #ifndef SGTTY
  47. #ifndef ITTY
  48. error Must define one of TERMIO, SGTTY or ITTY
  49. #endif
  50. #endif
  51. #endif
  52.  
  53. #ifndef NULL
  54. #include <stdio.h>
  55. #endif
  56.  
  57.  
  58. /* Process groups.
  59.  * Related jobs are placed in the same process group.  The terminal may
  60.  * then be asociated with whichever process group is desired (eg. for job
  61.  * control).  To set the process group / terminal association one may use
  62.  * the ioctl(TIOCSPGRP) or ioctl(TIOCGPGRP) to examine it.
  63.  */
  64.  
  65. /* Save terminal status.
  66.  * Fills the given buffer with the current terminal settings.
  67.  * The terminal settings may be restored to the values given in the buffer
  68.  * by passing the buffer to the term_restore function.
  69.  * Returns 0 for success, -1 for failure.
  70.  */
  71.  
  72. public int Term_Save(fd, buf)
  73. int fd;
  74. TERM_STRUCT *buf;
  75. {
  76.     if (buf==NULL) return(-1);
  77. #ifdef TERMIO
  78.     return(ioctl(fd, TERM_GET, buf));
  79. #endif /* TERMIO */
  80. #ifdef SGTTY
  81.     /* Should be more in here but I don't know what */
  82.     return(ioctl(fd, TERM_GET, buf));
  83. #endif /* SGTTY */
  84. #ifdef ITTY
  85.     ioctl(fd, TIOCGETP, &buf->sgttyb);
  86.     ioctl(fd, TIOCGETC, &buf->tchars);
  87.     ioctl(fd, TIOCGLTC, &buf->ltchars);
  88.     ioctl(fd, TIOCLGET, &buf->lmode);
  89. #endif /* ITTY */
  90. }
  91.  
  92.  
  93. /* Restore terminal status.
  94.  * Restore the terminal to the state given by the contents of the buffer
  95.  * (which was filled by term_save).  The buffer is not deallocated so
  96.  * you must do that yourself.
  97.  * Returns 0 for success or -1 for error.
  98.  */
  99.  
  100. public int Term_Restore(fd, buf)
  101. int fd;
  102. TERM_STRUCT *buf;
  103. {
  104.     if (buf==NULL) return(-1);
  105. #ifdef TERMIO
  106.     return(ioctl(fd, TERM_SET, buf));
  107. #endif /* TERMIO */
  108. #ifdef SGTTY
  109.     /* Should be more in here but I don't know what */
  110.     return(ioctl(fd, TERM_SET, buf));
  111. #endif /* SGTTY */
  112. #ifdef ITTY
  113.     ioctl(fd, TIOCSETP, &buf->sgttyb);
  114.     ioctl(fd, TIOCSETC, &buf->tchars);
  115.     ioctl(fd, TIOCSLTC, &buf->ltchars);
  116.     ioctl(fd, TIOCLSET, &buf->lmode);
  117. #endif /* ITTY */
  118. }
  119.  
  120.  
  121. /* Set raw mode.
  122.  * Set the terminal into raw mode.
  123.  * This involves setting:
  124.  * Punctual input (clear ICANON and set MIN and TIME),
  125.  * No character mapping (clear OPOST, INLCR, ICRNL, IUCLC, ISTRIP),
  126.  * No flow control (clear IXON),
  127.  * No control characters (clear BRKINT, ISIG),
  128.  * No echo (clear ECHO).
  129.  * Returns 0 for success or -1 for error.
  130.  */
  131.  
  132. public int Term_Raw(fd)
  133. int fd;
  134. {
  135.     TERM_STRUCT settings;
  136.  
  137.     if (Term_Save(fd, &settings)<0) return(-1);
  138. #ifdef TERMIO
  139. /*    settings.c_iflag &= ~(INLCR | ICRNL | IUCLC | ISTRIP | IXON | BRKINT);*/
  140.     settings.c_iflag &= ~(INLCR | ICRNL | IUCLC | ISTRIP | BRKINT);
  141.     settings.c_oflag &= ~OPOST;
  142.     settings.c_lflag &= ~(ICANON | ISIG | ECHO);
  143.     settings.c_cc[TERM_MIN] = 1;        /* read() will block indefinitely */
  144.     settings.c_cc[TERM_TIME] = 0;
  145. #endif /* TERMIO */
  146. #if defined ITTY || defined SGTTY
  147.     settings.sgttyb.sg_flags |= RAW;
  148.     settings.sgttyb.sg_flags &= ~(CRMOD | ECHO | LCASE | CBREAK | TANDEM);
  149. #endif /* ITTY or SGTTY */
  150.     Term_Restore(fd, &settings);
  151.     return(0);
  152. }
  153.  
  154.  
  155. /* Set cooked mode.
  156.  * Take the terminal out of raw mode (ie. back to cooked/canonical mode)
  157.  * Returns 0 for success or -1 for error.
  158.  */
  159.  
  160. public int Term_NonRaw(fd)
  161. int fd;
  162. {
  163.     TERM_STRUCT settings;
  164.  
  165.     if (Term_Save(fd, &settings)<0) return(-1);
  166. #ifdef TERMIO
  167.     /* Don't put IUCLC back unless you want upper case made lower case */
  168. /*    settings.c_iflag |= (INLCR | ICRNL | ISTRIP | IXON | BRKINT);*/
  169.     settings.c_iflag |= (INLCR | ICRNL | ISTRIP | BRKINT);
  170.     settings.c_oflag |= OPOST;
  171.     settings.c_lflag |= (ICANON | ISIG | ECHO);
  172.     settings.c_cc[TERM_MIN] = 4;        /* VEOF */
  173.     settings.c_cc[TERM_TIME] = 0;       /* VEOL */
  174. #endif /* TERMIO */
  175. #if defined ITTY || defined SGTTY
  176.     settings.sgttyb.sg_flags &= ~RAW;
  177.     /* Don't add LCASE back in */
  178.     /* Do we want cooked or cbreak?  Cooked is most likely so no CBREAK */
  179.     settings.sgttyb.sg_flags |= (CRMOD | ECHO | TANDEM);
  180. #endif /* ITTY or SGTTY */
  181.     Term_Restore(fd, &settings);
  182.     return(0);
  183. }
  184.  
  185.  
  186. public int Term_NoEcho(fd)
  187. int fd;
  188. {
  189.     TERM_STRUCT settings;
  190.  
  191.     if (Term_Save(fd, &settings)<0) return(-1);
  192. #ifdef TERMIO
  193.     settings.c_lflag &= ~(ECHO);
  194. #endif /* TERMIO */
  195. #if defined ITTY || defined SGTTY
  196.     settings.sgttyb.sg_flags &= ~(ECHO);
  197. #endif /* ITTY or SGTTY */
  198.     Term_Restore(fd, &settings);
  199.     return(0);
  200. }
  201.